/*
 * Copyright (C) 2018 YunYu, Inc. All Rights Reserved.
 */
package com.yunyu.kingman.base.exception;

import org.apache.commons.lang3.StringUtils;

/**
 * @author lihui
 * @data 2018/8/8 下午11:16
 */
public class SystemException extends RuntimeException {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -8615103443717498766L;

    /**
     * 异常代码
     */
    private final ErrorCode resultCode;

    /**
     * 构造函数
     *
     * @param resultCode
     */
    public SystemException(ErrorCode resultCode) {
        this.resultCode = resultCode;
    }

    /**
     * 构造函数
     *
     * @param resultCode
     * @param message
     */
    public SystemException(ErrorCode resultCode, String message) {
        super(message);
        this.resultCode = resultCode;
    }

    /**
     * 构造函数
     *
     * @param resultCode
     * @param e
     */
    public SystemException(ErrorCode resultCode, Throwable e) {
        super(e);
        this.resultCode = resultCode;
    }

    /**
     * 构造函数
     *
     * @param resultCode
     * @param message
     * @param e
     */
    public SystemException(ErrorCode resultCode, String message, Throwable e) {
        super(message, e);
        this.resultCode = resultCode;
    }

    /**
     * /**
     * 获取异常代码
     *
     * @return property value of resultCode
     */
    public ErrorCode getErrorCode() {
        return resultCode;
    }

    /**
     * @see java.lang.Throwable#getMessage()
     */
    @Override
    public String getMessage() {
        if (null == super.getMessage()) {
            return StringUtils.EMPTY;
        } else {
            return super.getMessage();
        }
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder retValue = new StringBuilder(" WardenException[");
        if (resultCode != null) {
            retValue.append("code=").append(resultCode.getCode()).append(',');
            retValue.append("description=").append(resultCode.getDescription()).append(',');
        }
        retValue.append("extraMessage=").append(getMessage());
        retValue.append(']');
        return retValue.toString();
    }
}
